package org.dromara.hm.mapper.saleOrder;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.dromara.common.mybatis.annotation.DataColumn;
import org.dromara.common.mybatis.annotation.DataPermission;
import org.dromara.hm.domain.HmSaleOrder;
import org.dromara.hm.domain.bo.HmSaleOrderBo;
import org.dromara.hm.domain.vo.saleOrder.HmSaleOrderVo;
import org.dromara.common.mybatis.core.mapper.BaseMapperPlus;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 销售订单Mapper接口
 *
 * @author zy Hu
 * @date 2024-12-15
 */

public interface HmSaleOrderMapper extends BaseMapperPlus<HmSaleOrder, HmSaleOrderVo> {

    @Override
    @DataPermission({
        @DataColumn(key = "deptName", value = "sale_dept"),
        @DataColumn(key = "userName", value = "sale_user")
    })
    default <P extends IPage<HmSaleOrderVo>> P selectVoPage(IPage<HmSaleOrder> page, Wrapper<HmSaleOrder> wrapper)
    {
        return BaseMapperPlus.super.selectVoPage(page, wrapper);
    }

    @DataPermission({
        @DataColumn(key = "deptName", value = "sale_dept"),
        @DataColumn(key = "userName", value = "sale_user")
    })
    @Override
    default List<HmSaleOrder> selectList()
    {
        return BaseMapperPlus.super.selectList();
    }

    List<HmSaleOrderVo> selectOrdersAfterDateWithNoTodo(
        @Param("triggerLocalDate") LocalDate triggerLocalDate,
        @Param("thresholdDays") int thresholdDays);

    void updateSaleOrderOwnerByCustomerIds(
        @Param("customerIds") List<Long> customerIds,
        @Param("newStaffId") Long effectiveNewStaffId,
        @Param("newStaffName") String effectiveNewStaffName,
        @Param("newStoreId") Long effectiveNewStoreId,
        @Param("newStoreName") String effectiveNewStoreName,
        @Param("updateBy") String operatorUsername,
        @Param("updateTime") Date now
    );

    /**
     * 查询待转移订单
     * @param customerIds
     * @return
     * 条件 .and(wrapper -> wrapper.ne(HmSaleOrder::getRefundStatus, 2).or().ne(HmSaleOrder::getReturnStatus, 3)
     */
    default List<HmSaleOrder> selectUnfinishedOrdersByCustomerIds(List<Long> customerIds) {
        return this.selectList(new LambdaQueryWrapper<HmSaleOrder>()
            .in(HmSaleOrder::getCustomerId, customerIds)
        );
    }

    /**
     * 根据自定义条件（回款额、剩余提货量）分页查询销售订单列表。
     *
     * @param page 分页对象
     * @param bo 查询条件
     * @return 销售订单分页列表
     */
    @DataPermission({
        @DataColumn(key = "deptName", value = "o.sale_dept"),
        @DataColumn(key = "userName", value = "o.sale_user")
    })
    IPage<HmSaleOrderVo> selectVoPageByCustomCriteria(IPage<HmSaleOrder> page, @Param("bo") HmSaleOrderBo bo);
}
